package model

import (
	"awesomeProject2/global"
	"gorm.io/gorm"
	"strconv"
	"time"
)

type Project struct {
	gorm.Model
	EndTime           time.Time `json:"endTime" gorm:"type:date"`
	ProjectFrom       string    `json:"projectFrom" gorm:"type:varchar(255)"`
	ProjectCategory   string    `json:"projectCategory" gorm:"type:varchar(255);"`
	ProjectLevel      string    `json:"projectLevel"  gorm:"type:varchar(255);"`
	ProjectName       string    `json:"projectName" gorm:"type:varchar(255);"`
	ProjectNumber     string    `json:"projectNumber" gorm:"type:varchar(255);"`
	ProjectTotalMoney float64   `json:"projectTotalMoney"  gorm:"type:double"`
	StartTime         time.Time `json:"startTime"  gorm:"type:date"`
	IsVisible         bool      `json:"is_visible"  gorm:"type:tinyint"`
	ProjectTime       string    `json:"projectTime"`
	Owner             string    `json:"owner"  gorm:"type:varchar(255)"`
	UserId            uint      `json:"userid"  gorm:"type:bigint"`
}

type AnnualProjectRecord struct {
	gorm.Model
	ProjectMoney            float64 `json:"projectMoney"  gorm:"type:double;"`
	ProjectAccumulatedFunds float64 `json:"projectAccumulatedFunds"  gorm:"type:double"`
	ProjectFile             string  `json:"projectFile"  gorm:"type:varchar(255);"`
	Year                    uint    `json:"year" gorm:"type:int;index:project_unique, unique"`
	ProjectId               uint    `json:"projectId" gorm:"type:bigint;index:project_unique, unique"`
}

func GetProjects(userid uint, pageSize int, pageNum int) (err error, list interface{}, total int) {
	var projects []Project
	var tt int64
	err = global.GVA_DB.Limit(pageSize).Offset((pageNum-1)*pageSize).Where("user_id = ?", userid).Find(&projects).Count(&tt).Error

	tmp := strconv.FormatInt(tt, 10)
	total, _ = strconv.Atoi(tmp)
	return err, projects, total
}

func GetProjectByLevel(projectlevel string, pageSize int, pageNum int) (err error, list interface{}, total int) {
	var projects []Project
	var tt int64
	err = global.GVA_DB.Limit(pageSize).Offset((pageNum-1)*pageSize).Where("project_level = ?", projectlevel).Find(&projects).Count(&tt).Error
	//err = global.GVA_DB.Table("paper_to_level").Select("paper_to_level.LevleId as level_id, paper_to_level.PaperId as paper_id, paper.PaperId as paper_id, paper.PaperName as paper_name").Joins("left join paper on paper_to_level.PaperId = paper.PaperId where paper_to_level.LevelId <> ? ", levelid).Find(&datas).Scan(&paperinfos).Error
	tmp := strconv.FormatInt(tt, 10)
	total, _ = strconv.Atoi(tmp)
	return err, projects, total

}

//获取在研项目
func GetAllOngoingProject(pageSize int, pageNum int) (err error, list interface{}, total int) {
	var projects []Project
	var tt int64
	nowtime := time.Now().Format("2006/01/02")
	err = global.GVA_DB.Limit(pageSize).Offset((pageNum-1)*pageSize).Where("? BETWEEN start_time AND end_time", nowtime).Find(&projects).Count(&tt).Error
	tmp := strconv.FormatInt(tt, 10)
	total, _ = strconv.Atoi(tmp)
	return err, projects, total
}

func ChangeProjectVisiblity(id uint) (err error) {
	var data Project
	global.GVA_DB.Where("id = ?", id).First(&data)
	err = global.GVA_DB.Model(&Project{}).Where("id = ?", id).Update("is_visible", !data.IsVisible).Error
	return err
}

func GetAllProjects3(id uint) (error, interface{}, int64) {
	var data []Project
	var tt int64
	err := global.GVA_DB.Where("user_id = ? ", id).Find(&data).Count(&tt).Error
	return err, data, tt
}

//统计总数目
func GetAllpj(year int) (error, int64) {
	var tt int64
	err := global.GVA_DB.Model(&Project{}).Where("year = ?", year).Count(&tt).Error
	return err, tt
}
